<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/materialdesignicons.min.css" rel="stylesheet">
<link href="css/style.min.css" rel="stylesheet">
  <link href="css/animate.css" rel="stylesheet">
  <!--对话框-->
  <link rel="stylesheet" href="js/jconfirm/jquery-confirm.min.css">
</head>
  
<body>
<div class="container-fluid p-t-15">
  
  <div class="row">
    <div class="col-lg-12">
      <div class="card">
        <div class="card-toolbar clearfix">
          <form class="pull-right search-bar" method="get" action="#!" role="form">
            <div class="input-group">
              <div class="input-group-btn">
                <input type="hidden" name="search_field" id="search-field" value="title">
                <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                标题 <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                  <li> <a tabindex="-1" href="javascript:void(0)" data-field="title">标题</a> </li>
                  <li> <a tabindex="-1" href="javascript:void(0)" data-field="cat_name">栏目</a> </li>
                </ul>
              </div>
              <input type="text" class="form-control" value="" name="keyword" id="keyword" placeholder="请输入名称" onblur="loadPageInfo('./productTypes/queryByPage')">
            </div>
          </form>
          <div class="toolbar-btn-action">
            <a class="btn btn-primary m-r-5" href="#!"><i class="mdi mdi-plus"></i> 新增</a>
            <a class="btn btn-success m-r-5" href="#!"><i class="mdi mdi-check"></i> 启用</a>
            <a class="btn btn-warning m-r-5" href="#!"><i class="mdi mdi-block-helper"></i> 禁用</a>
            <a class="btn btn-danger" href="#!"><i class="mdi mdi-window-close"></i> 删除</a>
          </div>
        </div>
        <div class="card-body">
          
          <div class="table-responsive">
            <table class="table table-bordered">
              <thead>
                <tr>
                  <th>类别名称</th>
                  <th>描述</th>
                  <th>操作</th>
                </tr>

              </thead>
              <tbody>
                <tr>
                  <td>家电</td>
                  <td>家用电器类别</td>
                  <td>
                    <div class="btn-group">
                      <a class="btn btn-xs btn-default" href="#!" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                      <a style="visibility: hidden;" class="btn btn-xs btn-default" href="#!" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
					  <a class="btn btn-xs btn-default" href="#!" title="删除" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
          <ul class="pagination" id="pagination">
          </ul>
 
        </div>
      </div>
    </div>
    
  </div>
  
</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/main.min.js"></script>

<!--对话框-->
<script src="js/jconfirm/jquery-confirm.min.js"></script>
<!--消息提示-->
<script src="js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="js/lightyear.js"></script>

<!--引入分页展示js-->
<script src="./js/pagination-comm.js"></script>

<script type="text/javascript">
  var pageVO = null;

$(function(){
    /*$('.search-bar .dropdown-menu a').click(function() {
        var field = $(this).data('field') || '';
        $('#search-field').val(field);
        $('#search-btn').html($(this).text() + ' <span class="caret"></span>');
    });*/

  //请求分页信息
  loadPageInfo("./productTypes/queryByPage");

});

  function loadPageInfo(url, page, size){
    //如果没有传递page信息则默认为第一页
    if(!page){
      page = 1;
    }
    //如果没有传size信息，默认为5条
    if(!size){
      size = 5;
    }

    //封装查询参数
    let param = {
      "page": page,
      "size": size
    }

    let keyword = $("#keyword").val();
    if(keyword && keyword.trim()){
      param.typeName = keyword;
    }

    //分页信息请求
    $.getJSON(url, param,
            function(returnVO){
              console.log("queryByPage: ", returnVO);
              //请求出错
              if(returnVO.code != 1){
                lightyear.notify(returnVO.msg, "danger", 5000);
                return;
              }

              //请求成功, 记录当前页信息
              pageVO = returnVO.content;
              //给全局页面对象捆绑请求地址
              pageVO.url = url;

              //显示信息
              showTableInfo();
              //显示分页
              setPagination();
            });


  }

  function showTableInfo(){
    //清空之前的数据
    $("tbody").empty();

    //遍历当前页的数据
    for(let item of pageVO.content){
      //拼接表格行
      let row = `
  			<tr>
                  <td>${item.typeName}</td>
                  <td>${item.typeDesc}</td>
                  <td>
                    <div class="btn-group">
                      <a class="btn btn-xs btn-default" href="javascript:jumpModPage(${item.typeId}, '${item.typeName}', '${item.typeDesc}')" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                      <a style="visibility: hidden;" class="btn btn-xs btn-default" href="#!" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
					  <a class="btn btn-xs btn-default" href="javascript:delById('./productTypes/deleteById/', ${item.typeId}, '${item.typeName}')" title="删除" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>
                    </div>
                  </td>
                </tr>
  		`;
      //添加到表格中
      $("tbody").append(row);
    }
  }

  /**
   * 跳转修改页面
   */
  function jumpModPage(typeId, typeName, typeDesc){
    //1-获取要修改的信息
    //2- 拼接要修改的信息为一个js对象
    let modType = {
      "typeId": typeId,
      "typeName": typeName,
      "typeDesc": typeDesc
    }
    //3- 转换修改的js对象为 JSON 放入 sessionStorage
    sessionStorage.setItem("modType", JSON.stringify(modType));

    //4- 跳转页面
    location.href = "pro_type_mod.html";

  }


</script>
</body>
</html>